/*************************************************************************************************
# Copyright (C), 20xx-20xx, xxx. Co., Ltd.
# File Name :		dev_misc.c
# Author : decorn		Version : V1.0		Date : 2017-08-10
# Description :
#	Device BSP
# Others :
#	None
# Function List :
#	
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#	2.
#************************************************************************************************/
#define FILE_ID_DEV_MISC

#include "dev_misc.h"
#include "stm32f0xx_gpio.h"
#include "main.h"


uint16_t	user_led = 0u;   //led输出数据初始化
uint16_t	user_led_dat[6u] = {0x0040u, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};


/*************************************************************************************************
# Function : wd_init
# Descriptions :
#	Initial port of watchdog clear.
# Input :
#	None
# Output :
#	None
# Return :
#	None
# Others :
#	None
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#---------------------------------------------------------*/
void wd_init(void)//看门够引脚初始化
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
	
	/* WD_CLR Port Initial */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
}

/*************************************************************************************************
# Function : wd_clr
# Descriptions :
#	Clear watchdog.
# Input :
#	None
# Output :
#	None
# Return :
#	None
# Others :
#	None
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#---------------------------------------------------------*/
void wd_clr(void)//硬件看门狗，每个周期翻转一次
{
	static bool_t	g_b8_wd_dat	= BFALSE;
	
	if(g_b8_wd_dat == BTRUE){
		g_b8_wd_dat	= BFALSE;
		GPIO_SetBits(GPIOC, GPIO_Pin_15);
	}
	else{
		g_b8_wd_dat	= BTRUE;
		GPIO_ResetBits(GPIOC, GPIO_Pin_15);
	}
}



/*************************************************************************************************
# Function : comm_tim_init
# Descriptions :
#	Initial common timer
# Input :
#	None
# Output :
#	None
# Return :
#	None
# Others :
#	None
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#---------------------------------------------------------*/
void comm_tim_init(void)//通用定时器TIM16的初始化，滤波，TIMEOUT等处理
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	uint16_t PrescalerValue = 0u;
	
	/* TIM16 clock enable */
	TIM_DeInit(TIM16);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);
	/* Compute the prescaler value */
	PrescalerValue = (uint16_t) (SystemCoreClock / 10000u) - 1u; /* 1/10000=100us */
	/* Time base configuration */
	TIM_TimeBaseStructure.TIM_Period = 0xFFFFu;  //设置自动重装载寄存器周期的值
	TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;   //设置时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_ClockDivision = 0u;    //时钟分割
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x00u;
	TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure);
	
	TIM_ARRPreloadConfig(TIM16, ENABLE);
	
	/* TIM6 ENABLE counter */
	TIM_Cmd(TIM16,  ENABLE);
}

/*************************************************************************************************
# Function : comm_tim_get_currt_cnt
# Descriptions :
#	Get value of common timer
# Input :
#	None
# Output :
#	None
# Return :
#	Timer counter value
# Others :
#	None
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#---------------------------------------------------------*/
uint16_t comm_tim_get_currt_cnt(void)//获取当前计数值
{
	return (uint16_t)TIM16->CNT;
}



/*************************************************************************************************
# Function : comm_tim_init
# Descriptions :
#	Initial common timer
# Input :
#	None
# Output :
#	None
# Return :
#	None
# Others :
#	None
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#---------------------------------------------------------*/
//void comm_tim_hbs_init(void)//通用定时器TIM17的初始化，心跳监控处理
//{
//	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
//	uint16_t PrescalerValue = 0u;
//	
//	/* TIM16 clock enable */
//	TIM_DeInit(TIM17);
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE);
//	/* Compute the prescaler value */
//	PrescalerValue = (uint16_t) (SystemCoreClock / 10000u) - 1u; /* 1/10000= 0.1ms */
//	/* Time base configuration */
//	TIM_TimeBaseStructure.TIM_Period = 0xFFFFu;  //设置自动重装载寄存器周期的值
//	TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;   //设置时钟频率除数的预分频值
//	TIM_TimeBaseStructure.TIM_ClockDivision = 0u;    //时钟分割
//	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数
//	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x00u;
//	TIM_TimeBaseInit(TIM17, &TIM_TimeBaseStructure);
//	
//	TIM_ARRPreloadConfig(TIM17, ENABLE);
//	
//	/* TIM6 ENABLE counter */
//	TIM_Cmd(TIM17,  ENABLE);
//}


/*************************************************************************************************
# Function : comm_tim_get_currt_cnt
# Descriptions :
#	Get value of common timer
# Input :
#	None
# Output :
#	None
# Return :
#	Timer counter value
# Others :
#	None
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#---------------------------------------------------------*/
//uint16_t comm_hbs_get_currt_cnt(void)//获取当前计数值
//{
//	return (uint16_t)TIM17->CNT;
//}

/*************************************************************************************************
# Function : comm_tim_calc_diff
# Descriptions :
#	Calculation difficult value of common timer
# Input :
#	None
# Output :
#	None
# Return :
#	Difficult value
# Others :
#	None
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#---------------------------------------------------------*/
uint32_t comm_tim_calc_diff(uint16_t *u16p_val)//计算间隔时间，并更新当前值
{
	uint16_t	u16_old_val		= 0u;
	uint16_t	u16_currt_val	= 0u;
	uint32_t	u32_diff_val	= 0u;
	
	u16_currt_val	= (uint16_t)TIM16->CNT;
	u16_old_val		= *u16p_val;
	*u16p_val		= u16_currt_val;
	if(u16_currt_val < u16_old_val)
	{
		u32_diff_val	= (0xFFFFu - u16_old_val) + u16_currt_val;
	}
	else
	{
		u32_diff_val	= (uint32_t)u16_currt_val;
		u32_diff_val	= u32_diff_val - u16_old_val;
	}
	
	return u32_diff_val;
}

/*************************************************************************************************
# Function : led_port_init
# Descriptions :
#	Initial LED port
# Input :
#	None
# Output :
#	None
# Return :
#	None
# Others :
#	None
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#---------------------------------------------------------*/
void led_port_init(void)//LED-IO初始化
{
	GPIO_InitTypeDef GPIO_InitStructure;
	/* Enable the LED Clock */
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC , ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = (uint32_t)(GPIO_Pin_0 |GPIO_Pin_1 | GPIO_Pin_11 | GPIO_Pin_12);
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = (uint32_t)(GPIO_Pin_13 | GPIO_Pin_14);
	GPIO_Init(GPIOC, &GPIO_InitStructure);	

  GPIO_SetBits(GPIOA, GPIO_Pin_0);	
	GPIO_SetBits(GPIOA, GPIO_Pin_1);
	GPIO_SetBits(GPIOA, GPIO_Pin_11);
	GPIO_SetBits(GPIOA, GPIO_Pin_12);
	GPIO_SetBits(GPIOC, GPIO_Pin_13);
	GPIO_SetBits(GPIOC, GPIO_Pin_14);
}

/*************************************************************************************************
# Function : led_set_red
# Descriptions :
#	Set Red LED status
# Input :
#	b8_is_light	: LED status
# Output :
#	None
# Return :
#	None
# Others :
#	None
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#---------------------------------------------------------*/
//void led_set_red(bool_t b8_is_light)//设置红灯
//{
//	if(b8_is_light == BTRUE){
//		GPIO_ResetBits(GPIOA, GPIO_Pin_11);
//	}
//	else{
//		GPIO_SetBits(GPIOA, GPIO_Pin_11);
//	}
//}



void led_set_LED_flash(uint8_t u8_led_flash )//设置闪烁
{
	static bool_t	g_b8_led	= BFALSE;
	static uint16_t g_u16_cnt =0u ;
	static uint16_t g_u16_time_old =0u ;
	uint32_t	u32_tmp_dat	= 0u;
	
		if (g_b8_led==BFALSE)
			{
				if(g_u16_cnt< 5000u)
	     	{
			     u32_tmp_dat = comm_tim_calc_diff(&g_u16_time_old);//
		       g_u16_cnt += (uint16_t)u32_tmp_dat; //
					
           if((u8_led_flash&0x01)==0x01) LED1(0); //灯1灭
					 if((u8_led_flash&0x02)==0x02) LED2(0); //灯2灭		 
					 if((u8_led_flash&0x04)==0x04) LED3(0); //灯3灭
           if((u8_led_flash&0x08)==0x08) LED4(0); //灯4灭	
					 if((u8_led_flash&0x10)==0x10) LED5(0); //灯5灭	
					 if((u8_led_flash&0x20)==0x20) LED6(0); //灯6灭	
		    }
			 else
			 {
			   g_b8_led  = BTRUE;
				 g_u16_cnt = 0u;
			 }	
			}
			else
			{
				if(g_u16_cnt< 5000u)
	     	{
			     u32_tmp_dat = comm_tim_calc_diff(&g_u16_time_old);//
		       g_u16_cnt += (uint16_t)u32_tmp_dat; //	
					 if((u8_led_flash&0x01)==0x01) LED1(1); //灯1亮
					 if((u8_led_flash&0x02)==0x02) LED2(1); //灯2亮	 
					 if((u8_led_flash&0x04)==0x04) LED3(1); //灯3亮
           if((u8_led_flash&0x08)==0x08) LED4(1); //灯4亮	
					 if((u8_led_flash&0x10)==0x10) LED5(1); //灯5亮	
					 if((u8_led_flash&0x20)==0x20) LED6(1); //灯5亮	
		    }
			 else
			 {
			   g_b8_led  = BFALSE;
				 g_u16_cnt = 0u;
			 }		
			}
//			if((u8_led_flash|0xFE)==0xFE) 	{g_u16_time_old=comm_tim_get_currt_cnt();} //灯1灭
//		  if((u8_led_flash|0xFD)==0xFD) 	{g_u16_time_old=comm_tim_get_currt_cnt();} //灯1灭
//			if((u8_led_flash|0xFB)==0xFB) 	{g_u16_time_old=comm_tim_get_currt_cnt();} //灯1灭
//		  if((u8_led_flash|0xF7)==0xF7) 	{g_u16_time_old=comm_tim_get_currt_cnt();} //灯1灭
//			if((u8_led_flash|0xEF)==0xEF) 	{g_u16_time_old=comm_tim_get_currt_cnt();} //灯1灭
//		  if((u8_led_flash|0xDF)==0xDF) 	{g_u16_time_old=comm_tim_get_currt_cnt();} //灯1灭
	  
}


/*************************************************************************************************
# Function : led_set_yellow
# Descriptions :
#	Set Yellow LED status
# Input :
#	b8_is_light	: LED status
# Output :
#	None
# Return :
#	None
# Others :
#	None
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#---------------------------------------------------------*/
void led_set_yellow(bool_t b8_is_light)//设置黄灯
{
	if(b8_is_light == BTRUE){
		GPIO_ResetBits(GPIOA, GPIO_Pin_12);
	}
	else{
		GPIO_SetBits(GPIOA, GPIO_Pin_12);
	}
}

/*************************************************************************************************
# Function : get_mcu_addr
# Descriptions :
#	Get MCU address
# Input :
#	None
# Output :
#	None
# Return :
#	MCU address
# Others :
#	None
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#---------------------------------------------------------*/
uint8_t get_mcu_addr(void)//获得MCU地址
{
	GPIO_InitTypeDef GPIO_InitStructure;
	uint8_t		u8_mcu_addr		= 0u;
	uint16_t	u16_port_val	= 0u;

	/* Enable the MCU_ADDR Clock */
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB , ENABLE);
	/* Configure MCU_ADDR pin as input */
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;
	/* MCU_ADDR Pin */
	GPIO_InitStructure.GPIO_Pin = (uint32_t)(GPIO_Pin_6 | GPIO_Pin_7);
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	/* Get MCU address value */
	u8_mcu_addr	= 0u;
	do{
		u16_port_val = GPIO_ReadInputData(GPIOB);
		u8_mcu_addr  = (uint8_t)((u16_port_val >> 6u) & 0x0003u);
	} while((u8_mcu_addr != ADDR_MCU_A) && (u8_mcu_addr != ADDR_MCU_B));
	
	return u8_mcu_addr;
}

/*************************************************************************************************
# Function : soft_dly_us
# Descriptions :
#	Software delay
# Input :
#	u32_dly	: delay value
# Output :
#	None
# Return :
#	None
# Others :
#	None
# History :
#	1. Author : decorn		Version : V1.0		Date : 2017-08-10
#		Modification: Create
#---------------------------------------------------------*/
void soft_dly_us(uint32_t u32_dly)//软件延时
{
	uint32_t u32_cnt	= 0u;
	uint32_t u32_us		= 0u;
	
	for(u32_us = 0u; u32_us < u32_dly; u32_us ++){
		for(u32_cnt = 0u; u32_cnt < SOFT_US_CNT; u32_cnt ++){
			(void)__nop();
		}
	}
}



void bsp_Led_state_zht( uint8_t led)   //led亮--
{
	uint8_t u8lv_led_dat	= 0;
	
	for(uint8_t i = 0; i<6 ; i++)
	{
		u8lv_led_dat = (led & (0x01 << i));
		switch(u8lv_led_dat)
		{
		case 0x01: LED1(1);break;
		case 0x02: LED2(1);break;
		case 0x04: LED3(1);break;
		case 0x08: LED4(1);break;
		case 0x10: LED5(1);break;
		case 0x20: LED6(1);break;
			default: break;
		}
		if(u8lv_led_dat == 0x00)
		{
			switch(i)
			{
				case 0x00: LED1(0);break;
				case 0x01: LED2(0);break;
				case 0x02: LED3(0);break;
				case 0x03: LED4(0);break;
				case 0x04: LED5(0);break;
				case 0x05: LED6(0);break;
				default: break;
			
			}
		}	
	}
}


/*********************************************************************************************************
*	Function : bsp_Led_state       
*	Descriptions : 点亮指定的LED灯。范围 0x3F LED灯全亮
*********************************************************************************************************/
//void bsp_Led_state( uint8_t u8_led_on,uint8_t u8_led_flash)   //chen-led亮
//{
//	if((u8_led_flash|0xFE)==0xFE)
//	{
//	  if((u8_led_on&0x01)==0x01) LED1(1);
//		if((u8_led_on|0xFE)==0xFE) LED1(0); //灯1灭
//	}
//	if((u8_led_flash|0xFD)==0xFD)
//	{
//	  if((u8_led_on&0x02)==0x02) LED2(1);
//		if((u8_led_on|0xFD)==0xFD) LED2(0); //灯1灭
//	}
//	
//	if((u8_led_flash|0xFB)==0xFB)
//	{
//	  if((u8_led_on&0x04)==0x04) LED3(1);
//		if((u8_led_on|0xFB)==0xFB) LED3(0); //灯1灭
//	}
//	if((u8_led_flash|0xF7)==0xF7)
//	{
//	  if((u8_led_on&0x08)==0x08) LED4(1);
//		if((u8_led_on|0xF7)==0xF7) LED4(0); //灯1灭
//	}
//	if((u8_led_flash|0xEF)==0xEF)
//	{
//	  if((u8_led_on&0x10)==0x10) LED5(1);
//		if((u8_led_on|0xEF)==0xEF) LED5(0); //灯1灭
//	}
//	if((u8_led_flash|0xDF)==0xDF)
//	{
//	  if((u8_led_on&0x20)==0x20) LED6(1);
//		if((u8_led_on|0xDF)==0xDF) LED6(0); //灯1灭
//	}
//}	

void bsp_Led_state( uint8_t u8_led_on,uint8_t u8_led_flash)   //led亮
{
	if((u8_led_flash|0xFE)==0xFE)
	{
	  if((u8_led_on&0x01)==0x01){ LED1(1);}
		if((u8_led_on|0xFE)==0xFE){ LED1(0);} //灯1灭
	}

	if((u8_led_flash|0xFD)==0xFD)
	{
	  if((u8_led_on&0x02)==0x02){ LED2(1);}
		if((u8_led_on|0xFD)==0xFD){ LED2(0); }//灯1灭
	}
	
	if((u8_led_flash|0xFB)==0xFB)
	{
	  if((u8_led_on&0x04)==0x04) LED3(1);
		if((u8_led_on|0xFB)==0xFB) LED3(0); //灯1灭
	}
	if((u8_led_flash|0xF7)==0xF7)
	{
	  if((u8_led_on&0x08)==0x08) LED4(1);
		if((u8_led_on|0xF7)==0xF7) LED4(0); //灯1灭
	}
	if((u8_led_flash|0xEF)==0xEF)
	{
	  if((u8_led_on&0x10)==0x10) LED5(1);
		if((u8_led_on|0xEF)==0xEF) LED5(0); //灯1灭
	}
	if((u8_led_flash|0xDF)==0xDF)
	{
	  if((u8_led_on&0x20)==0x20) LED6(1);
		if((u8_led_on|0xDF)==0xDF) LED6(0); //灯1灭
	}
}	
	


//void set_led_flash(uint8_t number)//设置LED灯闪烁  zhou
//{
//	uint8_t   u8_led;
//	uint32_t	u32_tmp_dat	= 0u;
//		 	
//	for(uint8_t y=0u; y<6u; y++)
//	{
//		u8_led = (number &(0x01 <<y ));	
//		if(u8_led != 0u)  //有值
//		{		
//		 if(g_st_ram1.run_auxiliary_data.sta_led[y].g_b8_red == BFALSE)
//		 {
//				if(g_st_ram1.run_auxiliary_data.sta_led[y].g_u16_cnt < 5000u)
//				{
//					 u32_tmp_dat = comm_tim_calc_diff(&g_st_ram1.run_auxiliary_data.sta_led[y].g_u16_time_old);
//					 g_st_ram1.run_auxiliary_data.sta_led[y].g_u16_cnt += (uint16_t)u32_tmp_dat; 
////							 bsp_Led_state(number);  //设置led灯为灭
//					if( u8_led == 0x00)
//					{
//					switch(y)
//						{
//						case 0x00: LED1(0);break;  //设置led灯为灭
//						case 0x01: LED2(0);break;
//						case 0x02: LED3(0);break;
//						case 0x03: LED4(0);break;
//						case 0x04: LED5(0);break;
//						case 0x05: LED6(0);break;
//							default:  break ;
//						}
//					}
//				}		
//				else 
//				{
//					g_st_ram1.run_auxiliary_data.sta_led[y].g_b8_red  = BTRUE;
//					g_st_ram1.run_auxiliary_data.sta_led[y].g_u16_cnt = 0u;
//				}
//		 }
//		 else 
//		 {
//		 if(g_st_ram1.run_auxiliary_data.sta_led[y].g_u16_cnt< 5000u) //5000
//				{
//					 u32_tmp_dat = comm_tim_calc_diff(&g_st_ram1.run_auxiliary_data.sta_led[y].g_u16_time_old);
//					 g_st_ram1.run_auxiliary_data.sta_led[y].g_u16_cnt += (uint16_t)u32_tmp_dat; 
//					// led_set_red(BTRUE); //设置led为亮
//					switch(u8_led)
//						{
//						case 0x01: LED1(1);break;  //设置led灯为亮
//						case 0x02: LED2(1);break;
//						case 0x04: LED3(1);break;
//						case 0x08: LED4(1);break;
//						case 0x10: LED5(1);break;
//						case 0x20: LED6(1);break;
//							default:  break ;
//						}
//				}
//			 else
//			 {
//				 g_st_ram1.run_auxiliary_data.sta_led[y].g_b8_red  = BFALSE;
//				 g_st_ram1.run_auxiliary_data.sta_led[y].g_u16_cnt = 0u;
//			 }		
//		 }			 
//	 }		
//	else 
//	{  
//		//led_set_red(BTRUE); //设置红灯为灭
//   if( u8_led == 0x00)
//			{
//			switch(y)
//				{
//				case 0x00: LED1(0);break;  //设置led灯为灭
//				case 0x01: LED2(0);break;
//				case 0x02: LED3(0);break;
//				case 0x03: LED4(0);break;
//				case 0x04: LED5(0);break;
//				case 0x05: LED6(0);break;
//					default:  break ;
//				}
//			}
//		g_st_ram1.run_auxiliary_data.sta_led[y].g_u16_time_old = comm_tim_get_currt_cnt();
//	} 
// }	
//}




